package com.shmn.gongjiajumain.service.impl;

import com.shmn.gongjiajumain.entity.ProductionLineEntity;
import com.shmn.gongjiajumain.entity.PurchaseIncomeSubmitEntity;
import com.shmn.gongjiajumain.entity.UserEntity;
import com.shmn.gongjiajumain.service.UserService;
import com.shmn.gongjiajumain.utils.DateUtil;
import com.shmn.gongjiajumain.vo.purchaseincome.PurchaseIncomeTableVo;
import com.shmn.gongjiajumain.vo.repair.RepairSubmitHistoryFormVo;
import com.shmn.gongjiajumain.vo.repair.RepairSubmitHistoryTableVo;
import com.shmn.gongjiajumain.vo.repair.RepairSubmitMineTableVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.Query;

import com.shmn.gongjiajumain.dao.RepairSubmitDao;
import com.shmn.gongjiajumain.entity.RepairSubmitEntity;
import com.shmn.gongjiajumain.service.RepairSubmitService;


@Service("repairSubmitService")
public class RepairSubmitServiceImpl extends ServiceImpl<RepairSubmitDao, RepairSubmitEntity> implements RepairSubmitService {

    @Autowired
    private UserService userService;

    @Override
    public PageUtils queryPage1(RepairSubmitHistoryFormVo repairSubmitHistoryFormVo) {
        /**
         * 条件查询+分页
         */
        Map<String, Object> params = new HashMap<>();
        params.put("page",repairSubmitHistoryFormVo.getPageIndex());
        params.put("limit",repairSubmitHistoryFormVo.getPageSize());
        params.put("key",repairSubmitHistoryFormVo.getKey());
        IPage<RepairSubmitEntity> page = this.page(
                new Query<RepairSubmitEntity>().getPage(params),
                new QueryWrapper<RepairSubmitEntity>()
        );

        /**
         * 构建并返回vo对象
         */
        PageUtils pageUtils = new PageUtils(page);
        List<RepairSubmitHistoryTableVo> repairSubmitHistoryTableVos = new ArrayList<>();
        Class<RepairSubmitEntity> clazz = RepairSubmitEntity.class;
        pageUtils.getList().forEach(item->{
            RepairSubmitHistoryTableVo repairSubmitHistoryTableVo = new RepairSubmitHistoryTableVo();
            BeanUtils.copyProperties(item,repairSubmitHistoryTableVo);
            String submitId = null;
            Date submitTime = null;
            try {
                Field field = clazz.getDeclaredField("submitId");
                Field field1 = clazz.getDeclaredField("submitTime");
                field.setAccessible(true);
                field1.setAccessible(true);
                submitId = (String) field.get(item);
                submitTime = (Date) field1.get(item);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
            System.out.println(submitId);
            repairSubmitHistoryTableVo.setSubmitName(userService.getById(submitId).getName());
            repairSubmitHistoryTableVo.setSubmitTime(DateUtil.formatDate(submitTime));
            repairSubmitHistoryTableVos.add(repairSubmitHistoryTableVo);
        });
        pageUtils.setList(repairSubmitHistoryTableVos);
        return pageUtils;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RepairSubmitEntity> page = this.page(
                new Query<RepairSubmitEntity>().getPage(params),
                new QueryWrapper<RepairSubmitEntity>()
        );
        List<RepairSubmitMineTableVo> repairSubmitMineTableVos = new ArrayList<>();
        PageUtils pageUtils = new PageUtils(page);
        Class<RepairSubmitEntity> clazz = RepairSubmitEntity.class;
        pageUtils.getList().forEach(item->{
            RepairSubmitMineTableVo repairSubmitMineTableVo = new RepairSubmitMineTableVo();
            BeanUtils.copyProperties(item,repairSubmitMineTableVo);
            Date submitTime = null;
            try {
                Field field = clazz.getDeclaredField("submitTime");
                field.setAccessible(true);
                submitTime = (Date) field.get(item);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
            repairSubmitMineTableVo.setSubmitTime(DateUtil.formatDate(submitTime));
            repairSubmitMineTableVos.add(repairSubmitMineTableVo);
        });
        pageUtils.setList(repairSubmitMineTableVos);
        return pageUtils;
    }

}